#coding:utf-8
# coding:utf-8
import json
import pickle
import random
import time


def login(list):
    print('>>>可登录用户：',list)
    username=input('请输入你的游戏名字：')
    index=0
    a = []
    for dic in list:
        a.append(dic['name'])
    while True:
        if username in a:
            print('>>>登录成功')
            play(list, index, dic)
            break
        else:
            print('>>>无该玩家')
            break


def new(list):
    print('>>>已有用户：',list)
    username = input('>>>请注册游戏名：(输入esc退出)')
    if username=='esc':
        main()
    elif not username or username.isspace():
        print('>>>不能为空！')
        new(list)
    for i in list:
        if i['name']==username:
            print('>>>该玩家已存在！请重新注册')
            new(list)

    dic = {'name': username, 'exp': 0, 'lv': 1, 'hp': 100}
    print('>>>注册成功，你的初始属性为：')
    print('>>>姓名:',username,'，经验值：0，等级：1，生命值：100')
    f=open('user.txt','wb')
    list.append(dic)
    pickle.dump(list,f)
    f.close()


def save(list,index,dic,dic1):
    f = open('user.txt', 'wb')
    list.insert(index, dic1)
    list.remove(dic)
    pickle.dump(list, f)
    f.close()

def play(list,index,dic):
    dic1=dic
    while True:
        cho=input('请输入你的操作：1.探险   2.休息   3.查看当前角色信息   4.存档退出')
        if cho=='1':
            ret=random.choice(range(1,101))
            a = random.randint(1, 3)
            if ret<=10:
                print('>>>终极Boss菜虚鲲,出现概率10%(打败后经验值+250)随机对玩家造成50,100,150伤害,击败后增加300HP')
                att=a*50
                #beat(list,dic,dic1,index,att)
                if dic1['hp'] > 0:
                    dic1['hp'] -= att
                    if dic1['hp'] >= 0:
                        dic1['exp'] += 250
                        lv = dic1['exp'] // 100
                        dic1['lv'] += lv
                        dic1['exp']-=lv*100
                        dic1['hp'] += 300
                        print('>>>恭喜击败菜虚鲲，当前hp为:', dic1['hp'])
                        if lv >= 1:
                            print('>>>恭喜升到%s级' % (dic1['lv']))
                            save(list, index, dic, dic1)
                            play(list, index, dic)
                    else:
                        dic1['hp'] = 0
                        print('>>>你被菜虚鲲击杀，探险失败！')
                        save(list, index, dic, dic1)
                        play(list, index, dic)
                else:
                    print('>>>你的血量不足，休息会再来吧！')

            elif ret<=30:
                print('>>>天青牛蟒,出现概率30%(打败后经验值+100)  随机对玩家造成30,45,60伤害')
                att = (a-1) * 15+30
                #beat(list,dic,dic1,index,att)
                if dic1['hp'] > 0:
                    dic1['hp'] -= att
                    if dic1['hp'] >= 0:
                        dic1['exp'] += 100
                        lv = dic1['exp'] // 100
                        dic1['lv'] += lv
                        dic1['exp'] -= lv * 100

                        print('>>>恭喜击败天青牛蟒，当前hp为:',dic1['hp'])
                        if lv >= 1:
                            print('>>>恭喜升到%s级' % (dic1['lv']))
                            save(list, index, dic, dic1)
                            play(list, index, dic)
                    else:
                        dic1['hp'] = 0
                        print('>>>你被天青牛蟒击杀，探险失败！')
                        save(list, index, dic, dic1)
                        play(list,index,dic)
                else:
                    print('>>>你的血量不足，休息会再来吧！')

            elif ret<=50:
                print('>>>深海魔鲸王,出现概率50%(打败后经验值+70)  随机对玩家造成12,24,36伤害')
                att = (a-1) * 12+12
                #beat(list,dic,dic1,index,att)
                if dic1['hp'] > 0:
                    dic1['hp'] -= att
                    if dic1['hp'] >= 0:
                        dic1['exp'] += 70
                        lv = dic1['exp'] // 100
                        dic1['lv'] += lv
                        dic1['exp'] -= lv * 100

                        print('>>>恭喜击败深海魔鲸王，当前hp为:',dic1['hp'])
                        if lv >= 1:
                            print('>>>恭喜升到%s级' % (dic1['lv']))
                            save(list, index, dic, dic1)
                            play(list, index, dic)
                    else:
                        dic1['hp'] = 0
                        print('>>>你被深海魔鲸王击杀，探险失败！')
                        save(list, index, dic, dic1)
                        play(list,index,dic)
                else:
                    print('>>>你的血量不足，休息会再来吧！')

            elif ret<=60:
                print('>>>三眼金猊,出现概率60%(打败后经验值+50)  随机对玩家造成9,18,27伤害')
                att= (a-1) * 9+9
                #beat(list,dic,dic1,index,att)
                if dic1['hp'] > 0:
                    dic1['hp'] -= att
                    if dic1['hp'] >= 0:
                        dic1['exp'] += 50
                        lv = dic1['exp'] // 100
                        dic1['lv'] += lv
                        dic1['exp'] -= lv * 100

                        print('>>>恭喜击败三眼金猊，当前hp为:',dic1['hp'])
                        if lv >= 1:
                            print('>>>恭喜升到%s级' % (dic1['lv']))
                            save(list, index, dic, dic1)
                            play(list, index, dic)
                    else:
                        dic1['hp'] = 0
                        print('>>>你被三眼金猊击杀，探险失败！')
                        save(list, index, dic, dic1)
                        play(list,index,dic)
                else:
                    print('>>>你的血量不足，休息会再来吧！')

            elif ret<=70:
                print('>>>凤尾鸡冠蛇,出现概率70%(打败后经验值+40)  随机对玩家造成7,14,21伤害')
                att= (a-1) * 7+7
                #beat(list,dic,dic1,index,att)
                if dic1['hp'] > 0:
                    dic1['hp'] -= att
                    if dic1['hp'] >= 0:
                        dic1['exp'] += 40
                        lv = dic1['exp'] // 100
                        dic1['lv'] += lv
                        dic1['exp'] -= lv * 100

                        print('>>>恭喜击败凤尾鸡冠蛇，当前hp为:',dic1['hp'])
                        if lv >= 1:
                            print('>>>恭喜升到%s级' % (dic1['lv']))
                            save(list, index, dic, dic1)
                            play(list, index, dic)
                    else:
                        dic1['hp'] = 0
                        print('>>>你被凤尾鸡冠蛇击杀，探险失败！')
                        save(list, index, dic, dic1)
                        play(list,index,dic)
                else:
                    print('>>>你的血量不足，休息会再来吧！')

            else:
                print('>>>史莱克,出现概率90%(打败后经验值+30) 随机对玩家造成5,10,15伤害')
                att= (a-1) * 5+5
                #beat(list,dic,dic1,index,att)
                if dic1['hp'] > 0:
                    dic1['hp'] -= att
                    if dic1['hp'] >= 0:
                        dic1['exp'] += 30
                        lv = dic1['exp'] // 100
                        dic1['lv'] += lv
                        dic1['exp'] -= lv * 100
                        print('>>>恭喜击败史莱克，当前hp为:', dic1['hp'])
                        if lv >= 1:
                            print('>>>恭喜升到%s级' % (dic1['lv']))
                            save(list, index, dic, dic1)
                            play(list, index, dic)
                    else:
                        dic1['hp'] = 0
                        print('>>>你被史莱克击杀，探险失败！')
                        save(list, index, dic, dic1)
                        play(list, index, dic)
                else:
                    print('>>>你的血量不足，休息会再来吧！')

        elif cho=='2':
            while True:
                time.sleep(0.5)
                bv=random.randint(10,20)#随机提升血量
                dic1['hp']+=bv
                print('>>>当前生命值为：',dic1['hp'])

                save(list,index,dic,dic1)

                slif=input('是否继续休息？ 是：1，否：任意键')
                if slif=='1':
                    continue
                else:
                    break
        elif cho=='3':
            print('>>>玩家:', dic['name'], ',经验值:',dic['exp'],',等级:',dic['lv'],',生命值:',dic['hp'])
        elif cho=='4':
            main()
            break
        else:
            print('>>>请输入正确的操作！')

def main():
    # 得到该游戏的用户list
    list = []
    try:
        f = open('user.txt', 'ab+')
        f.seek(0)
        list = pickle.load(f)  # 如果文件为空，此处异常
        f.close()
    except Exception as e:
        print('>>>初始化list')
        list = []
        f = open('user.txt', 'wb')
        pickle.dump(list, f)  # 将初始化的信息写进文件
        f.close()
    print('----欢迎来到勇者大冒险游戏！----')

    while True:
        cho = input(' 1：读取存档，2：新建存档，3：退出（其他操作）')
        if cho == '1':
            login(list)
        elif cho == '2':
            new(list)
        else:
            print('>>>游戏退出。。。')
            break



if __name__ == '__main__':
    main()
